﻿using KTWpm.Management.Api.Infrastructure;
using KTWpm.Management.Domain;
using KTWpm.Management.Domain.Entities;
using KTWpm.Management.Domain.ValueObjects;

namespace KTWpm.Management.Api.Application;

public class ManagementApplicationService(IBreadService breadService, ManagementDbContext dbContext)
{
    public async Task Handle(CreatePetCommand command)
    {
        var breedId = new BreedId(command.BreedId, breadService);
        var newPet = new Pet(command.Id, command.Name, command.Age, command.Color, command.SexOfPet, breedId);

        await dbContext.Pets.AddAsync(newPet);
        await dbContext.SaveChangesAsync();
    }

    public async Task Handle(SetWeightCommand command)
    {
        var pet = await dbContext.Pets.FindAsync(command.Id);
        if (pet == null)
        {
            throw new ArgumentException("Pet not found.");
        }
        pet.SetWeight(command.Weight, breadService);

        await dbContext.SaveChangesAsync();
    }
}
